package com.ktjy.controller;

import com.ktjy.entity.Flower;
import com.ktjy.entity.SysRole;
import com.ktjy.entity.SysUser;
import com.ktjy.service.FlowerService;
import com.ktjy.service.SysRoleService;
import com.ktjy.service.SysUserService;
import com.ktjy.utils.Constants;
import com.ktjy.utils.Page;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;
import java.util.concurrent.Flow;

@Controller
@RequestMapping("/main/flower")
public class FlowerController extends BaseController{
    Logger logger= LoggerFactory.getLogger(FlowerController.class);

    @Autowired
    FlowerService flowerService;

    @RequestMapping("/page")
    public String page(Flower flower,
                       @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                       Model model){
        try {
            //分页查询用户信息
            Page<Flower> page = flowerService.findPage(flower, pageNo, pageSize);
            model.addAttribute("pageInfo",page);

            //查询条件回显
            model.addAttribute("condition",flower);
        } catch (Exception e) {
            logger.error("page 方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "flower/page";
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @GetMapping("/save")
    public String toSave(Model model){
        return "flower/save";
    }

    @PostMapping("/save")
    public String doSave(Flower flower){
        try {
            boolean ok =false;
            if(flower.getFlowerId()==null){//新增
                 ok = flowerService.save(flower);
            }else{//修改
                 ok = flowerService.update(flower);
            }

            if(ok){
                //跳转到列表;重定向到分页方法
                return redirect("/main/flower/page");
            }else{
                //跳回新增页面
                return "flower/save";
            }
        } catch (Exception e) {
            logger.error("save方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
    }

    //rest风格通俗理解 是一种定义请求路劲的方式，他可以直接在路劲上传递参数
    //跟传统的请求路劲的区别
    //路劲?参数名=参数值   vs  路劲/参数名
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id,Model model){
        try {
            Flower flower = flowerService.findById(id);
            model.addAttribute("flower",flower);
        } catch (Exception e) {
            logger.error("view方法执行发生错误。",e);
            throw new RuntimeException(e);
        }
        return "flower/view";
    }

    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Long id,Model model){
        try {
            Flower flower = flowerService.findById(id);
            model.addAttribute("flower",flower);
        } catch (Exception e) {
            logger.error("toEdit方法执行发生错误。",e);
            throw new RuntimeException(e);
        }

        return "flower/save";
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object del(@PathVariable("id") Long id){
        Map<String,Object> map=new HashMap<>();
        try {
            boolean ok = flowerService.delete(id);  //调用service的方法进行数据删除
            if(ok){
                map.put("code",200);
                map.put("msg","删除成功");
            }else{
                map.put("code",500);
                map.put("msg","删除失败");
            }
        } catch (Exception e) {
            logger.error("del方法执行发生错误。",e);
            map.put("code",500);
            map.put("msg","删除失败");
        }
        return map;
    }

    @GetMapping("/accountExist")
    @ResponseBody
    public Object accountExist(@RequestParam("name") String name){
        Map<String,Object> map=new HashMap<>();
        try {
            Flower flower = flowerService.findByName(name);  //调用service的方法进行数据删除
            if(flower!=null){
                map.put("exist",1);
            }else{
                map.put("exist",0);
            }
        } catch (Exception e) {
            logger.error("accountExist方法执行发生错误",e);
            map.put("exist",-1);
            map.put("error","系统繁忙，请稍后再试");
        }
        return map;
    }

}
